package org.agilelang.editor;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import org.agilelang.editor.tool.InflextorFile;
import org.agilelang.editor.tool.InfoProject;
import org.agilelang.editor.tool.Util;
import org.agilelang.editor.view.tab.ViewTab;
import org.agilelang.editor.view.tree.GradientTitleBorder;
import org.agilelang.editor.view.tree.ViewTree;

public class ViewFrame extends JFrame {

    private ViewTree tree;
    private ViewTab tabs;
    private ViewToolBar tool;
    private ViewMenu menu;
    private JTextArea textdebug;
    private String pathProject;
    public String pathGenerator = "";
    private String title = "AgileLang 1.4";
    private InfoProject project = new InfoProject();

    public ViewFrame() {
        tree = new ViewTree(this);
        tabs = new ViewTab(this);
        menu = new ViewMenu(this);
        tool = new ViewToolBar(menu,this);
       
        setTitle(title);
        this.setIconImage(Util.getIconImage("org/agilelang/editor/resources/icon_logo_32.png"));
        initComponent();
    }

    private void initComponent() {
        //--MENU--
        this.setJMenuBar(menu.getMenuBar());
        //--TOOLBAR--
        getContentPane().add(tool.getToolBar(), BorderLayout.NORTH);
        //--TREE--
        JComponent st_tree = new JScrollPane(tree);
        st_tree.setBorder(new GradientTitleBorder("Project", new Color(0x418EDC), new Color(0x6B91B8), Color.WHITE, 25));
        MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {                
                int selRow = tree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
                if (selRow != -1) {
                    if (e.getClickCount() == 1) {
                    } else if (e.getClickCount() == 2) {
                        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selPath.getLastPathComponent();
                        System.out.println("Doble click :" + parent);
                        if(parent.toString().endsWith(".agile")||parent.toString().endsWith(".xml")||parent.toString().endsWith(".sql")){
                            tabs.addTab(parent.toString());
                        }
                        
                    }
                }
            }
        };
        tree.addMouseListener(ml);
        tree.empty();
        //--CENTER--
        JSplitPane splitPane = new javax.swing.JSplitPane(javax.swing.JSplitPane.HORIZONTAL_SPLIT, st_tree, tabs);
        splitPane.setOneTouchExpandable(false);
        splitPane.setDividerLocation(200);
        getContentPane().add(splitPane, java.awt.BorderLayout.CENTER);
        //--SOUTH--
        textdebug = new JTextArea(5, 20);
        /*JScrollPane scrollPane = new JScrollPane(textdebug, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        textdebug.setLineWrap(true);
        textdebug.setEditable(false);
        JPanel statusPanel = new JPanel();
        statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        statusPanel.setPreferredSize(new Dimension(this.getWidth(), 16));
        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
        JLabel statusLabel = new JLabel("AgileLang");
        statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
        statusPanel.add(statusLabel);
        JPanel panel_south = new JPanel();
        panel_south.setLayout(new BorderLayout());
        panel_south.add(scrollPane, BorderLayout.NORTH);
        panel_south.add(statusPanel, BorderLayout.SOUTH);
        getContentPane().add(panel_south, BorderLayout.SOUTH);*/
        //----------------------------------------------------------------------
        addWindowListener(windowListener());
    }

    public WindowAdapter windowListener() {
        WindowAdapter action = new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                /*
                 * if (editor.isChange()) { saveDocument(); } 
                 */
                System.exit(0);
            }
        };
        return action;
    }

    public void appendDebug(String message) {
        textdebug.append(message + "\n");
    }

    public void clearDebug() {
        textdebug.setText("");
    }

    public InfoProject getProject() {
        return project;
    }

    public void setProject(InfoProject project) {
        this.project = project;
    }

    public JTextArea getTextdebug() {
        return textdebug;
    }

    public void setTextdebug(String textdebug) {
        this.textdebug.append(textdebug+"\n");
    }

    public String getPathProject() {
        return pathProject;
    }

    public void setPathProject(String getPathProject) {
        this.pathProject = getPathProject;
    }

    public void rename_tab(String name_old, String name_new) {
        tabs.rename_tab(name_old, name_new);
    }

    public void newproject() {
        this.setTitle(this.project.name + " - " + this.title);
        this.project.version = "1.4";
        this.project.save(this.pathProject);
        this.tree.newProject();
        this.tabs.newProject();
        textdebug.setText("");
    }

   public  void openproject() {
        this.setTitle(this.project.name + " - " + this.title);
        this.tree.openProject();
        this.tabs.openProject();
        textdebug.setText("");
    }

    public void closeproject() {
        this.setTitle(this.title);
        this.tree.closeProject();
        this.tabs.closeProject();
        textdebug.setText("");
    }

    public void save() {
        this.tabs.saveAll();
    }

    public void format() {
        this.tabs.saveAll();
        this.tabs.closeProject();
        List<File> files = addFiles(null, new File(pathProject));

        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
            if (file.isFile() && file.getName().endsWith(".agile")) {
                System.out.println("File inflextor" + file.getAbsolutePath());
                InflextorFile inflextorFile = new InflextorFile(file.getAbsolutePath());
            }
        }
    }

    public List<File> addFiles(List<File> files, File dir) {
        if (files == null) {
            files = new LinkedList<File>();
        }

        if (!dir.isDirectory()) {
            files.add(dir);
            return files;
        }

        for (File file : dir.listFiles()) {
            addFiles(files, file);
        }
        return files;
    }

    public void newFile(String fileName) {
        this.tree.newFile(fileName);
    }

    public void createFile(String name,String file) {
        this.tree.newFile(name,file);
    }
}
